//		ID : Xerxes
//      "I have not failed, I have just found 10000 ways that won't work.
//


#include <iostream>
#include <algorithm>
#include <vector>
#include <sstream>
#include <fstream>
#include <string>
#include <list>
#include <map>
#include <set>
#include <queue>
#include <stack>
#include <functional>
#include <bitset>

#include <ctime>
#include <cassert>
#include <cstdio>
#include <cmath>
#include <cstring>
#include <climits>
#include <cstring>
#include <cstdlib>

using namespace std;

#define VI vector< int >
#define CI( x ) scanf("%d",&x)
#define CL( x ) scanf("%lld",&x)
#define CD( x ) scanf("%lf",&x )
#define fo(i,st,ed) for(int i = st ; i < ed ; ++i )
#define foE(i,st,ed) for(int i = st ; i <= ed ; ++i )
#define foit(i, x) for (typeof((x).begin()) i = (x).begin(); i != (x).end(); i++)
#define bip system("pause")
#define Int long long
#define pb push_back
#define SZ(X) (int)(X).size()
#define LN(X) (int)(X).length()
#define mk make_pair
#define f first
#define s second
#define SET( ARRAY , VALUE ) memset( ARRAY , VALUE , sizeof(ARRAY) )
#define tp pair< int, pair<int,int> >
#define mmk( x, y , z ) mk( x, mk( y, z ) )

inline void wait( double seconds ){
    double endtime = clock()+( seconds* CLOCKS_PER_SEC );
    while( clock() < endtime ){
        ;
    }
}
map< string, int > id;
int ncity;
int nedges;
int best;
int K=1;
struct NODE{
		int to;
		int cost;
		NODE(int to_, int cost_):
		to(to_),cost(cost_){;}
};
vector< vector<NODE> > cango;
inline void Read(){
	CI(ncity);
	id.clear();
	fo(i,0,ncity){
		string name;
		cin>>name;
		id[name]=i;
	}
	cango.assign(ncity+5, vector<NODE>() );
	
	CI(nedges);
	fo(i,0,nedges){
		string from,to;
		int cost;
		cin>>from>>to>>cost;
		int from_ = id[from];
		int to_ = id[to];
		cango[from_].pb( NODE(to_,cost) );
	}
	
}
// from , stopovers
int D[105][105];
int F[105][105];
#define inf 1023456789


inline void dj(int sx, int sy, int mn){
	//ini start
	SET(F,0);
	fo(i,0,105){
		fo(j,0,105){
			D[i][j] = inf;
		}
	}
	//ini ends
	best = inf;
	set< tp , less<tp > > pq ;
	pq.insert( mmk(0, 0, 0 ) );
	D[0][0]=0;
	
	while( !pq.empty() ){
		int dist1  = pq.begin()->f;
		int father = pq.begin()->s.f;
		int stopover1 = pq.begin()->s.s;
		
		if( father==sy && stopover1 <= mn+1 ){
			best = min( best, dist1);
		}
		pq.erase( pq.begin() );
		if( F[father][stopover1] )continue;
		
		for(int i=0,j=SZ(cango[father]); i<j; ++i ){
			int child = cango[father][i].to;
			int dist2 = cango[father][i].cost;
			int stopover2 = stopover1+1;
			
			if( F[child][stopover2]==0 && ( dist1+dist2 < D[child][stopover2] ) ){
				D[child][stopover2] = dist1+dist2;
				pq.insert( mmk(D[child][stopover2], child,stopover2) );
			}
		}
		
		F[father][stopover1]=1;
	}
}
inline void Proc(){
	int Q;
	CI(Q);
	
	while(Q--){
		int mn;
		CI(mn);
		dj(0,ncity-1,mn);
		if( best <inf )
			printf("Total cost of flight(s) is $%d\n",best);
		else
			printf("No satisfactory flights\n");
	}
	
}
int main(){
    #ifndef ONLINE_JUDGE
        freopen("in","rt",stdin);
    #endif
    int cases=1;
    for( CI(cases) ; cases ; --cases,++K ){
        Read();
        if( K!=1 ){
			printf("\n");
		}
		printf("Scenario #%d\n",K);
        Proc();
    }

return 0;
}


